package ai.tasks;

import ai.AIConstant;
import ai.Trackable;
import ai.PathFinding;
import ai.AIUtil;
import ai.engine.AIResource;
import ai.engine.AIUnit;
import ai.engine.Task;
import ai.engine.TaskDoer;
import ai.engine.factories.TaskFactory;
import engine.UnitAction;
import engine.data.UnitType;
import util.ArrayList;
import util.List;

public class ProtectUnitTask extends Task {

	public ProtectUnitTask(short id, Trackable target) {
		super(id, TaskFactory.PROTECT_UNIT_TASK, AIUnit.class, "ProtectUnit", "",
				AIConstant.PRIORITY_STRONG, target, AIUtil.getCurrentAIP().protectUnitMin,
		AIUtil.getCurrentAIP().protectUnitMax);
	}

	public AIUnit getTarget() {
		return (AIUnit) target;
	}

	public boolean isTaskSuitable(TaskDoer possibleTaskDoer) {
		AIUnit possibleUnitDoer = (AIUnit) possibleTaskDoer;
		return UnitType.getAP(possibleUnitDoer.type) > 0;
	}

	public short getFormula(TaskDoer possibleTaskDoer) {
		//AIUnit possibleUnitDoer = (AIUnit) possibleTaskDoer;
		return (short) (this.priority + AIUtil.getCurrentAIP().carefulness);
	}

	public byte getAction(TaskDoer taskDoer) {
		AIUnit unitDoer = (AIUnit) taskDoer;
		if (!this.assignedResources.contains(unitDoer))
			return UnitAction.SKIP;
		if (!this.getTarget().played)
			return UnitAction.WAIT;
		return PathFinding.getNextDirection(PathFinding.TYPE_QUICKEST,
				unitDoer, getTarget());
	}

	public List getAdditionalResourcesNeeded() {
		List resourcesNeeded = new ArrayList();
		AIResource resource; 
		for (int i = this.getAssignedResources().size(); i < this.resourcesMin; i++) {
			resource = new AIResource(getTarget(), AIUtil.getBestDefenseUnitType());		
			resourcesNeeded.add(resource);
		}
		return resourcesNeeded;
	}
	
	public boolean isObjectiveAchieved() {
		return true; // fuit un tour et puis voila
	}

}
